package com.cuit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.mapper.BookMapper;
import com.cuit.mapper.UserMapper;
import com.cuit.pojo.User;
import com.cuit.pojo.UserQuery;
import com.cuit.result.R;
import com.cuit.service.UserService;
import io.swagger.models.Model;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;
    //查询
    @GetMapping("/selectById")
    public String selectById(ModelMap model,@RequestParam(value = "pn", defaultValue = "1") Integer pn ){

          Page<User> userPage=new Page<>(pn,5);
          Page<User> page=userService.page(userPage,null);
          model.addAttribute("page",page);

//        UserQuery userQuery=new UserQuery();
//        IPage<User> pageParam=new Page<>(1,5);
//        userMapper.selectPage(pageParam,null);
//        List<User> userList=pageParam.getRecords();
//        model.addAttribute("page",userList);

        return "admin/widgets-data";
    }

    //搜索
    @GetMapping("/select")
    public String select(ModelMap model,@RequestParam String username,@RequestParam(value = "pn", defaultValue = "1") Integer pn ){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("username",username);
        Page<User> userPage=new Page<>(pn,5);
        Page<User> page=userService.page(userPage,queryWrapper);
        model.addAttribute("page",page);

//        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        queryWrapper.like("username",username);
//        model.addAttribute("page",queryWrapper);
        System.out.println("来了");
        return "admin/widgets-data";

    }


    //删除
    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable Long id, RedirectAttributes attributes){
        int delete=userMapper.deleteById(id);
        if(delete!=0){
            attributes.addFlashAttribute("message","删除用户成功");
            return "redirect:/user/selectById";
        }else{
            attributes.addFlashAttribute("message","删除用户失败");
            return "redirect:/user/selectById";
        }
    }

    //修改
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable Long id,ModelMap model){
        model.addAttribute("user",userMapper.selectById(id));
        System.out.println("来了");
        return "admin/widgets-simple";
    }

    @RequestMapping("/edit")
    public String edit(User user){
        boolean b=userService.updateById(user);
        System.out.println("来了2");
        return "redirect:/user/selectById";
    }
//    //添加
//    @RequestMapping("/insertUser")
//    @ResponseBody
//    public R insertUser(){
//        User user=new User();
//        user.setUsername("LGZ");
//        user.setEmail("15645156@qq.com");
//        user.setPassword("123456");
//        user.setAvatar("null");
//        user.setPhone("1654156613");
//        user.setCreate_time("2022-08-09");
//        user.setSex("1");
//        user.setAboutme("ni hao");
//        boolean save = userService.save(user);
//
//        if(save){
//            System.out.println(save);
//            return R.ok();
//        }else{
//            return R.error();
//        }
//    }

    //分页查询
    @RequestMapping("/selectF")
    @ResponseBody
    public R selectF(){
        IPage<User> pageParam=new Page<>(1,5);
        userMapper.selectPage(pageParam,null);
        List<User> records=pageParam.getRecords();
        if(records!=null){
            System.out.println("当前页:"+pageParam.getCurrent());
            System.out.println("每页数据内容："+pageParam.getRecords());
            System.out.println("每页显示记录:"+pageParam.getSize());
            System.out.println("总记录数:"+pageParam.getTotal());
            System.out.println("总页数:"+pageParam.getPages());
            System.out.println("是否有下一页:"+((Page<User>) pageParam).hasNext());
            System.out.println("是否有上一页:"+((Page<User>) pageParam).hasPrevious());
            return R.ok().data("records",records);
        }else{
            return R.error();
        }
    }
}
